import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:flutter_keyboard_visibility/flutter_keyboard_visibility.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

class RegistController extends GetxController {

  var suffixIconsVisible = false.obs;
  var pwVisible = false.obs;
  var rePwVisible = false.obs;
  var accountController = TextEditingController().obs;
  var passwordCodeController = TextEditingController().obs;
  var rePasswordCodeController = TextEditingController().obs;

  var isSubmitBtnEnable = false.obs;
  var checkBoxChecked = false.obs;

  // late StreamSubscription<bool> keyboardSubscription;
  var scrollController = ScrollController().obs;

  @override
  void onInit() {
    super.onInit();

   /* keyboardSubscription = KeyboardVisibilityController().onChange.listen((bool visible) {
      print('Keyboard visibility update. Is visible: $visible');
      if(visible) {
        //scrollToBottom();
        Future.delayed(Duration(milliseconds: 500), () {
          scrollToBottom();
        });

      }
    });*/

  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    super.onClose();
    scrollController.value.dispose();
    // keyboardSubscription.cancel();
  }


  void scrollToBottom() {
    /*double top = scrollController.value.position.maxScrollExtent;
    scrollController.value.jumpTo(
      top,
    );*/
    scrollController.value.animateTo(
      scrollController.value.position.maxScrollExtent,
      duration: const Duration(seconds: 2),
      curve: Curves.easeOut,
    );
  }

  void setSuffixIconsVisible(bool visible) {
    suffixIconsVisible.value = visible;
  }

  void onTextFieldChange(v) {
    if (v != null && v != '') {
      suffixIconsVisible.value = true;
    } else {
      suffixIconsVisible.value = false;
    }

    //检查btn提交按钮是否可以点击.
    if(simpleChecking()) {
      isSubmitBtnEnable.value = true;
    } else {
      isSubmitBtnEnable.value = false;
    }
  }

  void onPwFieldChange(v) {
    //检查btn提交按钮是否可以点击.
    if(simpleChecking()) {
      isSubmitBtnEnable.value = true;
    } else {
      isSubmitBtnEnable.value = false;
    }
  }

  bool simpleChecking() {
    return accountController.value.text != '' && passwordCodeController.value.text != '' && rePasswordCodeController.value.text != '';
  }

  void onCheckBoxChange() {
    if(simpleChecking()) {
      isSubmitBtnEnable.value = true;
    } else {
      isSubmitBtnEnable.value = false;
    }
  }

  void onTapUserAgreementTask() {

  }

  void onTapPrivacyPolicyTask() {

  }


}
